********************************************************************************
* Graph Median Inflation Expectations 
********************************************************************************

clear
clear matrix
set more off
set scheme s1color
estimates clear
graph drop _all
set matsize 2500
log close _all
graph set window fontface "Times New Roman"

** Set Directory
cd "../Do"
** Set Haver Directory
// set haverdir "Haver", perm

********************************************************************************
** LOAD AND CLEAN DURABLES DATA
********************************************************************************

use ../Data/matched_data_durables_jun2018_baseline.dta, clear

*labelling the 3 different durables spending variables; all are composites; one is nominal; another is "real" deflated using a single (appliances) cpi; another is "real" deflated on a good-by-good basis (in previous merge file)
la var durables "nominal durable goods spending"
la var durables_real1 "real durable goods spending, single deflator"
la var durables_real2 "real durable goods spending, separate deflators"

drop ethnicity

recode mort (5=0)
recode stocks (5=0)
recode retacct (5=0)
recode howner (5=0)

* Recode some expectations variables from the Inflation surveys. 

* unemployment dummies
recode q6 (1=1) (2 3 = 0), gen(unemp_increase)
recode q6 (3=1) (1 2 = 0), gen(unemp_decrease)

gen conditions_12m = q2a
* note everybody who says "other" is in separate category
recode q2a (1=1) (2 3 = 0), gen(conditions_12m_better)
recode q2a (2=1) (1 3 = 0), gen(conditions_12m_worse)

gen interestrate_12m = q7
recode interestrate_12m (1=1) (2 3 = 0), gen(intrate_12m_up)
recode interestrate_12m (3=1) (1 2 = 0), gen(intrate_12m_down)

gen bconditions_12m = q4
* note everybody who says "other" is in separate category
recode q4 (1=1) (2 3 = 0), gen(bconditions_12m_better)
recode q4 (2=1) (1 3 = 0), gen(bconditions_12m_worse)

* house price forecasts

gen hppoint = .
replace hppoint = 0 if q41==3
replace hppoint = q42 if q41==1
replace hppoint = -q42 if q41==2
* there are some extreme outliers (in the tens of thousands)
replace hppoint = . if abs(hppoint)>=200

la var hppoint "House price expectation"

*** Prepare some additional descriptives
* Race isn't reported in all periods. 
preserve
collapse (mean) race , by(prim_key)
sort prim_key
tempfile race
save `race'
restore

drop race
sort prim_key
merge m:1 prim_key using `race'
tab _merge
drop _merge
drop if prim_key==""

recode race (1=1) (nonmissing = 0), gen(white)
gen nonwhite = 1-white
recode gender (2=1) (1=0), gen(female)
recode highesteducation (4 9 = 0) (10/16 = 1), gen(coll)

* q31s* gives codes. Kind of odd that they've created separate variables. 
*MB: odd to generate employed variable, because everyone in sample is employed--however some also say they are retired (?) 
gen employed = . 
replace employed=1 if q31s1==1
replace employed = 0 if (q31s2==2 | q31s3==3 | q31s4==4 | q31s5==5 | q31s6==6 | q31s7==7) & q31s1!=1

* generate currently retired variable.
*some say they are retired even though all are employed; 
drop retired 
gen retired = . 
replace retired = 1 if q31s5==5
replace retired = 0 if (q31s1==1 | q31s2==2 | q31s3==3 | q31s4==4 | q31s6==6 | q31s7==7) & q31s5!=5
tab retired

gen gas_expect = . 
replace gas_expect = 0 if q47a==3
replace gas_expect = q47a_higher if q47a==1
replace gas_expect = -q47a_lower if q47a==2

*replace howner=0 if howner==.
*adds ten people to homeowner status; there are still some with howner_fix==0 who have positive mortgage payment, but missing data for has mortgage and/or mortgage amount
*two observations have howner==0 and mort==1 (say they're not a homeowner but they say they have a mortgage); not important so not recoding them for now

drop if mort==0 & amtmort!=. & amtmort>100
*fixing the mort indicator to impute plausible values for people based on other information
replace mort = 0 if (howner!=1 | mortgage==0) & mort==.
replace mort = 1 if (howner==1 | (mortgage>0 & mortgage!=.)) & mort==.

la var mort "Has Mortgage"

*MB: generating quasi-continuous income variable based on midpoint of ranges of annual household income variables (familyincome and familyincome_part2)
rename familyincome inc2
rename familyincome_part2 inc2_2
drop if inc2==.
*here is the income variable: "new_faminc"
gen new_faminc=.
replace new_faminc=2500 if inc2==1
replace new_faminc=6250 if inc2==2
replace new_faminc=8750 if inc2==3
replace new_faminc=11250 if inc2==4
replace new_faminc=13750 if inc2==5
replace new_faminc=17500 if inc2==6
replace new_faminc=22500 if inc2==7
replace new_faminc=27500 if inc2==8
replace new_faminc=32500 if inc2==9
replace new_faminc=37500 if inc2==10
replace new_faminc=45000 if inc2==11
replace new_faminc=55000 if inc2==12
replace new_faminc=67500 if inc2==13
replace new_faminc=87500 if inc2==14 & (inc2_2==1 | inc2_2==.)
*above accounts for one person with inc2==14 and inc2_2 missing; not sure why that's the case but I asigned them the lowest category of income over $75000
replace new_faminc=112500 if inc2==14 & inc2_2==2
replace new_faminc=162500 if inc2==14 & inc2_2==3
replace new_faminc=237500 if inc2==14 & inc2_2==4

gen log_new_faminc=log(new_faminc)

sort prim_key quarter
*bringing in SAMPLE WEIGHTS, to quarterly data: warning, we might lose observations if the set requiring weights has changed ; this will affect our ability to run regs using non-employed types (can only do unweighted) 
merge m:1 prim_key using ../Data/qweights_pooled
*these are the reg weights, the full sample weights will still be called weight_full
*weight variable is called "weight_samp" to indicate the weights were designed for the regression sample
*134 observations dropped that didn't merge with a weight_samp
drop if _merge!=3
drop _merge

*MB December 2019: key juncture: impose different sample restrictions and use different real/nominal durables
*********************
*define regression sample before recentering any variables: dropping extreme values
*below should drop top 2 highest values of durables spending; I doubt it makes any difference 
*JIMIN: try turning on and off the following restrictions
drop if durables>20000  | durables_real1>20000 | durables_real2>20000
drop if prim_key=="5041140:1"
drop if mortgage>200000 & mortgage!=.
drop if d_inflmedian>35
drop if d_longinflmedian>35
drop if hppoint<-50
**end of optional restrictions 

*assigning locals for weights (can turn on or off in regression)
local weights "[pweight=weight_samp]"
local weights_full "[pweight=weight_full]"
local pwfile "_pw"
*drop those with missing values for regressors
la var d_inflmedian "Inflation Expectation (SR)"
la var d_infliqr "Inflation Uncertainty (SR)"
la var d_longinflmedian "Inflation Expectation (MR)"
la var d_longinfliqr "Inflation Uncertainty (MR)"
la var lag_IE "Lagged Inflation Expectation (SR)"
la var lag_infl_iqr "Lagged Infl. Uncertainty (SR)"
la var hppoint "House price expectation"
la var gas_expect "Gas price expectation"

*variable for sum of monthly payments--interactions between this variable and IE may be included in some models
gen payments=mortgage+car if howner==1
replace payments=rent+car if howner!=1
gen log_payments=.
replace log_payments=log(payments) if payments>0
replace log_payments=0 if payments==0
la var log_payments "Fixed Mnthly Paymnts (Log)"
*drop any observations with extreme value for payments (110,000): only if running a regression interacting with payments: actually none dropped here (unlike nondurables)
drop if payments>100000

*defining sample; removing those with missing values
drop if weight_samp==.
drop if d_inflmedian==.
drop if d_infliqr==.
drop if durables==.
*dropping lagged IE: only need if we include lag IE in regression 
drop if lag_IE==.
drop if lag_infl_iqr==.
*new income variable: new_faminc is recode of categorical variables familyincome and familyincome_part2; former variable was earnings last month and highly unreliable
drop if new_faminc==.
drop if intrate_12m_up==.
drop if intrate_12m_down==.
drop if unemp_increase==.
drop if unemp_decrease==.
drop if rw_expect==.
drop if d_wageiqr==.
drop if rage==.
drop if nonwhite==.
drop if female==.
drop if coll==.
drop if retired==.
drop if mort==.
*below results in loss of 300+ observations--results are robust not imposing this restriction and omitting hppoint from regressions
drop if hppoint==.
*below drops 111 observations--robustness applies again 
drop if howner==.

destring prim_key, generate(id_new) ignore(":")

*generate total durables spending within household
*add to below: use durables_real1 and durables_real2 instead--number of observations of "durables" per household will be identical to that for either "durables_real1" or "durables_real2", so no need to repeat below for alternate versions
egen tot_durables=total(durables), by(id_new)
sum tot_durables, d
*below drops 142 observations associated with households who never purchased durables under period of observation (reported zero spending on durables) 
drop if tot_durables==0
*generate variable that equals 1 in all cases, to sum to determine observations per person
gen pre_obs=1
*generate sum of observations per person
egen obs2=total(pre_obs), by(id_new)
sum obs2, d

*restrict on having nonzero durables spending in at least one period (based on total durables spending within household) 
*define sample  based on sufficient observations
gen durables_sample_hp=(obs2>=3)
tempfile dur
save `dur'

********************************************************************************
* Create Durables Data for Figure
********************************************************************************

** Graph median inflation expectations at monthly and quarterly frequency
use `dur', clear
gen exp_qdate = qofd(expectations_date)
collapse (p50) d_inflmedian if durables_sample_hp==1 [pweight=weight_samp], by(exp_qdate)
rename d_inflmedian d_inflmedian_q
gen exp_mdate = mofd(dofq(exp_qdate)) + 1 //added for readability
drop if exp_qdate == tq(2009q3) //| exp_qdate == tq(2012q4)
tempfile dur_exp_quarter
save `dur_exp_quarter'

use `dur', clear
gen exp_mdate = mofd(expectations_date)
collapse (p50) d_inflmedian if durables_sample_hp==1 [pweight=weight_samp], by(exp_mdate)
rename d_inflmedian d_inflmedian_m
tempfile dur_exp_month
save `dur_exp_month'

use `dur_exp_quarter', clear
merge 1:1 exp_mdate using `dur_exp_month', nogen
format exp_mdate %tm
format exp_qdate %tq
sort exp_mdate
tsset exp_mdate
la var d_inflmedian_m "Median inflation expectations by month"
la var d_inflmedian_q "Median inflation expectations by quarter"
la var exp_mdate "Month"
tw (connected d_inflmedian_m d_inflmedian_q exp_mdate, name("g1", replace)), legend(cols(1)) title("Median Inflation Expectations for the Durables Sample")
// graph save "../Figures/inf1.gph", replace
// graph export "../Figures/durables_sample_inflation_expectations.png", as(png) replace

********************************************************************************
** LOAD AND CLEAN NONDURABLES DATA
********************************************************************************
clear
clear matrix
set more off
set scheme s1color
estimates clear
graph drop _all
set matsize 2500
log close _all

use ../Data/matched_data_nondurables_jun2018_baseline.dta, clear
*formerly: use matched_data_monthly_jan2016.dta

*list of spending categories that will add up to nondurables (first version excludes only sports, which is excluded because it has durable goods in it)
#delimit ;
local spendcats "
electricity water heatingfuel phonecable housecleaningproducts housecleaningservice
gardenproducts gardenservice clothing personalcare drugs healthcareservices medsupplies entertainment
hobbies personalservices otherchildspending foodhome foodout gasoline";
#delimit cr

*based on content of "spendcats" above, nondurables defined as all included in that group except sports
egen nondurables = rowtotal(`spendcats')

*not that nondurables has one outlier value over 29000 (for monthly spending on nondurable goods/services) 
*before running some regressions, extreme values for nondurables will be omitted; a small number of zeroes will be dropped

drop ethnicity

recode mort (5=0)
recode stocks (5=0)
recode retacct (5=0)
recode howner (5=0)

* Recode some expectations variables from the Inflation surveys. 

* unemployment dummies
recode q6 (1=1) (2 3 = 0), gen(unemp_increase)
recode q6 (3=1) (1 2 = 0), gen(unemp_decrease)

gen conditions_12m = q2a
* note everybody who says "other" is in separate category
recode q2a (1=1) (2 3 = 0), gen(conditions_12m_better)
recode q2a (2=1) (1 3 = 0), gen(conditions_12m_worse)

gen interestrate_12m = q7
recode interestrate_12m (1=1) (2 3 = 0), gen(intrate_12m_up)
recode interestrate_12m (3=1) (1 2 = 0), gen(intrate_12m_down)

gen bconditions_12m = q4
* note everybody who says "other" is in separate category
recode q4 (1=1) (2 3 = 0), gen(bconditions_12m_better)
recode q4 (2=1) (1 3 = 0), gen(bconditions_12m_worse)

* house price forecasts
gen hppoint = .
replace hppoint = 0 if q41==3
replace hppoint = q42 if q41==1
replace hppoint = -q42 if q41==2
* there are some extreme outliers (in the tens of thousands)
replace hppoint = . if abs(hppoint)>=200
la var hppoint "House price expectation"

* Deflation: using 2012q1 PCUN (nondurables CPI): generate a new variable so regressions can be run on nominal and real data alternatively
* from Arman: note that PCUN was redefined in the haver do file to give 2012q1 dollars

rename spend_month date_monthly
sort date_monthly
merge m:1 date_monthly using "../Data/haver_m.dta"

tab _merge
drop if _merge==2
drop _merge

*MB: my simple deflation command: just deflating my main measure of nondurables
gen nondurables_real=nondurables/PCUN
rename date_monthly spend_month
*note real values are only a little higher on average than nominal values

*** Prepare some additional descriptives
* Race isn't reported in all periods. 
preserve
collapse (mean) race , by(prim_key)
sort prim_key
tempfile race
save `race'
restore

drop race
sort prim_key
merge m:1 prim_key using `race'
tab _merge
drop _merge
drop if prim_key==""

recode race (1=1) (nonmissing = 0), gen(white)
gen nonwhite = 1-white
recode gender (2=1) (1=0), gen(female)
recode highesteducation (4 9 = 0) (10/16 = 1), gen(coll)

* q31s* gives codes. Kind of odd that they've created separate variables. 
*MB: odd to generate employed variable, because everyone in sample is employed--however some also say they are retired (?) 
gen employed = . 
replace employed=1 if q31s1==1
replace employed = 0 if (q31s2==2 | q31s3==3 | q31s4==4 | q31s5==5 | q31s6==6 | q31s7==7) & q31s1!=1

* generate currently retired variable.
*odd that some are retired even though all are employed; how do retired people have wage growth expectations? they may be working a small job anyway
drop retired 
gen retired = . 
replace retired = 1 if q31s5==5
replace retired = 0 if (q31s1==1 | q31s2==2 | q31s3==3 | q31s4==4 | q31s6==6 | q31s7==7) & q31s5!=5
tab retired

*replace howner=0 if howner==.
*adds ten people to homeowner status; there are still some with howner_fix==0 who have positive mortgage payment, but missing data for has mortgage and/or mortgage amount
*two observations have howner==0 and mort==1 (say they're not a homeowner but they say they have a mortgage); not important so not recoding them for now

*the mortgage dummy is non-missing for over 2000 observations; however the mortgage balance (amtmort) is observed only for 990 people
*tab mort
*reasonable recode of mortgage dummy based on other information
replace mort = 0 if (howner!=1 | mortgage==0) & mort==.
replace mort = 1 if (howner==1 | (mortgage>0 & mortgage!=.)) & mort==.
la var mort "Mortgage Indicator"

*MB: generating quasi-continuous income variable based on midpoint of ranges of annual household income variables (familyincome and familyincome_part2)
rename familyincome inc2
rename familyincome_part2 inc2_2
drop if inc2==.
*here is the income variable: "new_faminc"
gen new_faminc=.
replace new_faminc=2500 if inc2==1
replace new_faminc=6250 if inc2==2
replace new_faminc=8750 if inc2==3
replace new_faminc=11250 if inc2==4
replace new_faminc=13750 if inc2==5
replace new_faminc=17500 if inc2==6
replace new_faminc=22500 if inc2==7
replace new_faminc=27500 if inc2==8
replace new_faminc=32500 if inc2==9
replace new_faminc=37500 if inc2==10
replace new_faminc=45000 if inc2==11
replace new_faminc=55000 if inc2==12
replace new_faminc=67500 if inc2==13
replace new_faminc=87500 if inc2==14 & (inc2_2==1 | inc2_2==.)
*above accounts for one person with inc2==14 and inc2_2 missing; not sure why that's the case but I asigned them the lowest category of income over $75000
replace new_faminc=112500 if inc2==14 & inc2_2==2
replace new_faminc=162500 if inc2==14 & inc2_2==3
replace new_faminc=237500 if inc2==14 & inc2_2==4

gen log_new_faminc=log(new_faminc)

sort prim_key spend_month
*bringing in SAMPLE WEIGHTS, to monthly data: warning, we might lose observations if the set requiring weights has changed ; this will affect our ability to run regs using non-employed types (can only do unweighted) 
merge m:1 prim_key using ../Data/mweights_pooled
*these are the reg weights, the full sample weights will still be called weight_full
*weight variable is called "weight_samp" to indicate the weights were designed for the regression sample
*195 observations dropped that didn't merge with a weight_samp
drop if _merge!=3
drop _merge

*define regression sample before recentering any variables
*drop people with extreme values for spending, inflation expectations, mortgage payment that looks like a mortgage balance
*outliers identified by Ali: turn on as alternative MB: the below prim_key doesn't look suspicious to me---this drop may have been related to the previous (bad) income variable
*drop if prim_key=="5041140:1"
*drops 
drop if mortgage>200000 & mortgage!=.
*drops 17 observations
drop if d_inflmedian>35
*drops 13 observations
drop if nondurables==0
*drops 1 observation
drop if nondurables>28000

*assigning locals for weights (can turn on or off in regression)
local weights "[pweight=weight_samp]"
local weights_full "[pweight=weight_full]"
local pwfile "_pw"
*drop those with missing values for regressors
la var d_inflmedian "Inflation Expectation"
la var d_infliqr "Inflation Uncertainty"
la var d_longinflmedian "Inflation Expectation"
la var d_longinfliqr "Inflation Uncertainty"
la var lag_IE "Lagged Inflation Expectation"
la var lag_infl_iqr "Lagged Infl. Uncertainty"

*variable for sum of monthly payments--interactions between this variable and IE may be included in some models
gen payments=mortgage+car if howner==1
replace payments=rent+car if howner!=1
gen log_payments=.
replace log_payments=log(payments) if payments>0
replace log_payments=0 if payments==0
la var log_payments "Monthly Payments (Log)"
*drop one observation with extreme value for payments (110,000): only if running a regression interacting with payments
drop if payments>100000

*defining sample; removing those with missing values
drop if weight_samp==.
drop if d_inflmedian==.
drop if d_infliqr==.
drop if nondurables==.
*dropping lagged IE: only need if we include lag IE in regression 
drop if lag_IE==.
drop if lag_infl_iqr==.
*new income variable: new_faminc is recode of categorical variables familyincome and familyincome_part2; former variable was earnings last month and highly unreliable
drop if new_faminc==.
drop if intrate_12m_up==.
drop if intrate_12m_down==.
drop if unemp_increase==.
drop if unemp_decrease==.
drop if rw_expect==.
drop if d_wageiqr==.
drop if rage==.
drop if nonwhite==.
drop if female==.
drop if coll==.
drop if retired==.
drop if mort==.
*alternative: comment out the last two drops and omit howner and hppoint from regressions 
*below results in loss of 300+ observations--will retain alternative and omit hppoint from the regression
drop if hppoint==.
*this drops 111 observations--can run alternative model without howner dummy and include these 
drop if howner==.

destring prim_key, generate(id_new) ignore(":")
 
*generate variable that equals 1 in all cases, to sum to determine observations per person
gen pre_obs=1
*generate sum of observations per person
egen obs=total(pre_obs), by(id_new)
sum obs, d
*define sample  based on sufficient observation
gen nondurables_sample_base=(obs>=3)

*generate variable that equals 1 in all cases, to sum to determine observations per person
gen pre_obs2=1
*generate sum of observations per person
egen obs2=total(pre_obs2), by(id_new)
sum obs2, d
***Important line here: define sample  based on sufficient observations
gen nondurables_sample_hp=(obs2>=3)

gen year2 = year(exp_date)
gen month2 = month(exp_date)
gen yearmo = ym(year2, month2)
format yearmo %tm
rename exp_date expectations_date

tempfile nondur 
save `nondur'

********************************************************************************
* Create Nondurables Data for Figure
********************************************************************************
* Graph median inflation expectations at monthly and quarterly frequency
use `nondur', clear
gen exp_qdate = qofd(expectations_date)
collapse (p50) d_inflmedian if nondurables_sample_hp==1 [pweight=weight_samp], by(exp_qdate)
rename d_inflmedian d_inflmedian_q
gen exp_mdate = mofd(dofq(exp_qdate)) + 1 //add 1 for readability of graph
tempfile non_exp_quarter
save `non_exp_quarter'

use `nondur', clear
gen exp_mdate = mofd(expectations_date)
collapse (p50) d_inflmedian if nondurables_sample_hp==1 [pweight=weight_samp], by(exp_mdate)
rename d_inflmedian d_inflmedian_m
tempfile non_exp_month
save `non_exp_month'

use `non_exp_quarter', clear
merge 1:1 exp_mdate using `non_exp_month', nogen
format exp_mdate %tm
format exp_qdate %tq
sort exp_mdate
tsset exp_mdate
la var d_inflmedian_m "Median inflation expectations by month"
la var d_inflmedian_q "Median inflation expectations by quarter"
la var exp_mdate "Month"
tw (connected d_inflmedian_m d_inflmedian_q exp_mdate, name("g2", replace)), legend(cols(1)) title("Median Inflation Expectations for the Nondurables Sample")
// graph save "../Figures/inf2.gph", replace 
// graph export "nondurables_sample_inflation_expectations.png", as(png) replace

// graph combine "../Figures/inf1" "../Figures/inf2", name("g3", replace) cols(1) iscale(.6) ysize(8)
// graph export "../Figures/sample_inflation_expections_2_panels.png", as(png) replace

********************************************************************************
** CREATE FIGURE
********************************************************************************

use `non_exp_quarter', clear
merge 1:1 exp_mdate using `non_exp_month', nogen
rename d_* d_*_non
tempfile non_exp
save `non_exp'

use `dur_exp_quarter', clear
merge 1:1 exp_mdate using `dur_exp_month', nogen
rename d_* d_*_dur
tempfile dur_exp
save `dur_exp'

use `dur_exp'
merge 1:1 exp_mdate using `non_exp'
format exp_mdate %tm
format exp_qdate %tq
sort exp_mdate
tsset exp_mdate
la var d_inflmedian_m_dur "Median inflation expectations by month, durables"
la var d_inflmedian_q_dur "Median inflation expectations by quarter, durables"
la var d_inflmedian_m_non "Median inflation expectations by month, nondurables"
la var d_inflmedian_q_non "Median inflation expectations by quarter, nondurables"
la var exp_mdate "Month"
tw (connected d_inflmedian_m* d_inflmedian_q* exp_mdate), legend(cols(1)) title("Median Inflation Expectations")
// graph export "../Figures/all_samples_inflation_expectations.png", as(png) replace

use `non_exp_quarter', clear
rename d_* d_*_non
merge 1:1 exp_qdate using `dur_exp_quarter', nogen
rename d_inflmedian_q d_inflmedian_q_dur
format exp_qdate %tq
tsset exp_qdate
la var exp_qdate "Quarter"
la var d_inflmedian_q_dur "RAND-ALP Median Inflation Expectation (Durables Sample)"
la var d_inflmedian_q_non "RAND-ALP Median Inflation Expectation (Nondurables Sample)"
tw (connected d_inflmedian_q* exp_qdate), legend(cols(1) region(lstyle(none)) /*size(medlarge)*/) title("") ylab(1(1)5) xlab(199(2)211, /*labsize(medlarge)*/ format("%tqCCYY!Qq")) ylab(,/*labsize(medlarge)*/) xtitle("") ///
/*graphregion(margin(large))*/ ytitle("Percent")
graph export "../Figures/figureA2.png", as(png) replace

set scheme s1mono

tw (connected d_inflmedian_q* exp_qdate), legend(cols(1) region(lstyle(none)) /*size(medlarge)*/) title("") ylab(1(1)5) xlab(199(2)211, /*labsize(medlarge)*/ format("%tqCCYY!Qq")) ylab(,/*labsize(medlarge)*/) xtitle("") ///
/*graphregion(margin(large))*/ ytitle("Percent")
graph export "../Figures/figureA2_bw.png", as(png) replace

** NOTE: ONLY ONE MONTH IN 2012Q4!!!

********************************************************************************
** Plot Median IE_bar per period
********************************************************************************

// foreach weight in "[pweight = weight_samp]" " " {
//
// if "`weight'" == " " {
// 	local lab = "_unweighted"
// }
//
// use `nondur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen IE_bar_l = IE_bar
// gen IE_bar_h = IE_bar
// collapse (p50) IE_bar (p25) IE_bar_l (p75) IE_bar_h if nondurables_sample_hp==1 `weight', by(spend_month)
// tw (connected IE_bar spend_month) (rcap IE_bar_h IE_bar_l spend_month), title("Median Within-Household Average" "Inflation Expectations, Nondurables") xlab(597(12)633) legend(order(1 "Median" 2 "25th - 75th percentile")) ///
// ytitle("Median Within-Household Expectation (%)") xtitle(Spend Month)
// graph export "../Figures/median_IE_bar_nondur_spend_month`lab'.png", as(png) replace
//
// use `nondur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen IE_bar_l = IE_bar
// gen IE_bar_h = IE_bar
// gen exp_mdate = mofd(expectations_date)
// format exp_mdate %tm
// collapse (p50) IE_bar (p25) IE_bar_l (p75) IE_bar_h if nondurables_sample_hp==1 `weight', by(exp_mdate)
// tw (connected IE_bar exp_mdate) (rcap IE_bar_h IE_bar_l exp_mdate), title("Median Within-Household Average" "Inflation Expectations, Nondurables") legend(order(1 "Median" 2 "25th - 75th percentile")) ///
// ytitle("Median Within-Household Expectation (%)") xtitle(Expectation Reported Month) xlab(597(12)633)
// graph export "../Figures/median_IE_bar_nondur_expectation_month`lab'.png", as(png) replace
//
// use `dur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen IE_bar_l = IE_bar
// gen IE_bar_h = IE_bar
// collapse (p50) IE_bar (p25) IE_bar_l (p75) IE_bar_h if durables_sample_hp==1 `weight', by(quarter)
// tw (connected IE_bar quarter) (rcap IE_bar_h IE_bar_l quarter), title("Median Within-Household Average" "Inflation Expectations, Durables") legend(order(1 "Median" 2 "25th - 75th percentile")) ///
// ytitle("Median Within-Household Expectation (%)") xtitle(Spend Quarter)
// graph export "../Figures/median_IE_bar_dur_quarter`lab'.png", as(png) replace
//
// use `dur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen IE_bar_l = IE_bar
// gen IE_bar_h = IE_bar
// gen exp_qdate = qofd(expectations_date)
// format exp_qdate %tq
// collapse (p50) IE_bar (p25) IE_bar_l (p75) IE_bar_h if durables_sample_hp==1 `weight', by(exp_qdate)
// tw (connected IE_bar exp_qdate) (rcap IE_bar_h IE_bar_l exp_qdate), title("Median Within-Household Average" "Inflation Expectations, Durables") legend(order(1 "Median" 2 "25th - 75th percentile")) ///
// ytitle("Median Within-Household Expectation (%)") xtitle(Expectation Reported Quarter)
// graph export "../Figures/median_IE_bar_dur_expectation_quarter`lab'.png", as(png) replace
//
// use `nondur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen exp_mdate = mofd(expectations_date)
// format exp_mdate %tm
// collapse (p50) IE_bar (p50) d_inflmedian if nondurables_sample_hp==1 `weight', by(exp_mdate)
// tw (connected IE_bar exp_mdate) (connected d_inflmedian exp_mdate), title("Median Inflation Expectations, Nondurables") legend(order(1 "Within-Household Average Inflation Expectation" 2 "Inflation Expectation") cols(1)) ///
// ytitle("Inflation Expectation (%)") xtitle(Expectation Reported Month) xlab(597(12)633)
// graph export "../Figures/median_IE_bar_d_inflmedian_nondur`lab'.png", as(png) replace
//
// use `dur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen exp_qdate = qofd(expectations_date)
// format exp_qdate %tq
// collapse (p50) IE_bar (p50) d_inflmedian if durables_sample_hp==1 `weight', by(exp_qdate)
// tw (connected IE_bar exp_qdate) (connected d_inflmedian exp_qdate), title("Median Inflation Expectations, Durables") legend(order(1 "Within-Household Average Inflation Expectation" 2 "Inflation Expectation") cols(1)) ///
// ytitle("Inflation Expectation (%)") xtitle(Expectation Reported Quarter)
// graph export "median_IE_bar_d_inflmedian_dur`lab'.png", as(png) replace
//
// use `nondur', clear
// egen IE_bar=mean(d_inflmedian), by(id_new)
// gen exp_mdate = mofd(expectations_date)
// format exp_mdate %tm
// collapse (p50) IE_bar (p50) d_inflmedian if nondurables_sample_hp==1 `weight', by(spend_month)
// tw (connected IE_bar spend_month) (connected d_inflmedian spend_month), title("Median Inflation Expectations, Nondurables") legend(order(1 "Within-Household Average Inflation Expectation" 2 "Inflation Expectation") cols(1)) ///
// ytitle("Inflation Expectation (%)") xtitle(Spend Month) xlab(597(12)633)
// graph export "../Figures/median_IE_bar_d_inflmedian_nondur_spend_month`lab'.png", as(png) replace
// }